package smartcarpool.ui;

import android.app.PendingIntent;
import com.example.smartcarpool.R;

import smartcarpool.smartspaceservice.SmartSpaceProfile;
import smartcarpool.logic.SmartSpaceUserProfile;
import smartcarpool.smartspaceservice.SmartSpaceService;
import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import smartcarpool.smartspaceservice.Triple;
import smartcarpool.smartspaceservice.TripleList;

import static smartcarpool.smartspaceservice.SmartSpaceService.*;

public class ButtonListenerSC implements OnClickListener {


	private String HOST = "192.168.0.77";
	private int PORT = 10010;
	private String SPACE_NAME = "X";
	private View view;
	private FragmentManager fragmentManager;
	private ServiceConnection smSpConn;
	private Intent intentService;
	private SmartSpaceService ssService;
    private PendingIntent pendingIntent;

	
	public ButtonListenerSC(FragmentManager frm, View view, PendingIntent pendingIntent) {
		this.fragmentManager = frm;
		this.view = view;
        this.pendingIntent = pendingIntent;
        intentService = new Intent(view.getContext().getApplicationContext(), SmartSpaceService.class);
        SmartSpaceProfile ssProfile = new SmartSpaceProfile(HOST, PORT, SPACE_NAME);
        intentService.putExtra(SmartSpaceService.SMART_SPACE_PROFILE, ssProfile);
        intentService.putExtra(MainActivity.PARAM_PENDING,pendingIntent);
	}

	@Override
	public void onClick(View button) {
		
		Log.d("Debug", "Button Clicked");

		//SmartSpaceInsert ssInteraction = new SmartSpaceInsert(HOST,PORT,SPACE_NAME); 
		//Thread ssThread = new Thread(ssInteraction);
		//ssThread.setDaemon(true);
		//ssThread.start();
        //SmartSpaceProfile ssProfile = new SmartSpaceProfile(HOST, PORT, SPACE_NAME);
        //SmartSpaceService ss_service = new SmartSpaceService("SS_Service", ssProfile);
		//intentService = new Intent(view.getContext().getApplicationContext(), SmartSpaceService.class);



        /*smSpConn = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                // TODO Auto-generated method stub
                Log.d("Debug", "try to get service");
                ssService = ((SmartSpaceService.SSBinder) binder).getService();
            }
        };
*/
		if (button.getId() == R.id.button1){
            //intentService = new Intent();
            SmartSpaceUserProfile userProfile = new SmartSpaceUserProfile(fragmentManager);
            intentService.putExtra(SMART_SPACE_COMMAND,INSERT_COMMAND);
            intentService.putExtra(TRIPLE_LIST, userProfile.toTripleList());
            button.getContext().startService(intentService);

          /*  //Context buttonContext = button.getContext().getApplicationContext();
            //Log.d("Debug", buttonContext.toString());
            PendingIntent pIntent = PendingIntent.getActivity(view.getContext().getApplicationContext(), 0, intentService, 0);
            //view.getContext().getApplicationContext().startService(intentService);
            view.getContext().getApplicationContext().bindService(intentService, smSpConn, Context.BIND_AUTO_CREATE);
            Log.d("Debug", ssService.toString());
			ssService.insertTriplesToSmartSpace(SmartSpaceService.collectInformationFromApp(fragmentManager));
			//ssInteraction.insertInformationInSS(
			//		SmartSpaceInsert.collectInformationFromApp(fragmentManager));
			//Log.i("Information", fr1.);
			//Spinner sp1 = (Spinner) fr1.getView().findViewById(R.id.spinner1);
			//Log.i("Information", sp1.getSelectedItem().toString());
			//Log.i("Information", pointStartText.getText().toString());
			//ssInteraction.insertInformationInSS();
			//Log.i("Information", status);
			//ssInteraction.insertInformationInSS(bundle);*/
		} else if (button.getId() == R.id.button4){
            SmartSpaceUserProfile userProfile = new SmartSpaceUserProfile(fragmentManager);
            intentService.putExtra(SMART_SPACE_COMMAND,QUERY_COMMAND);
            Triple query = new Triple(null, "vehicle", null);
            TripleList queryTripleList = new TripleList();
            queryTripleList.add(query);
            intentService.putExtra(TRIPLE_LIST, queryTripleList);
            button.getContext().startService(intentService);
        } else if (button.getId() == R.id.button5){
            SmartSpaceUserProfile userProfile = new SmartSpaceUserProfile(fragmentManager);
            intentService.putExtra(SMART_SPACE_COMMAND,SUBSCRIBE_COMMAND);
            Triple subscribe = new Triple(null, "vehicle", null);
            TripleList subscribeTripleList = new TripleList();
            subscribeTripleList.add(subscribe);
            intentService.putExtra(TRIPLE_LIST, subscribeTripleList);
            button.getContext().startService(intentService);
        }
		
	}

}
